package com.xiaoxing.salesclient.mvp.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaoxing.salesclient.mvp.ui.entity.GridMenuItem;

import java.util.ArrayList;

import me.jessyan.armscomponent.commonres.utils.ViewUtils;
import xiaoxing.com.salesclient.R;

/**
 * @author 小星 QQ:753940262
 * @class describe
 * @time 2018/8/9 0009 13:37
 */
public class GridMenuAdapter extends ArrayAdapter<GridMenuItem> {

    private Context mContext;
    private int layoutResourceId;
    private ArrayList<GridMenuItem> mGridData = new ArrayList<GridMenuItem>();


    public GridMenuAdapter(Context context, int resource, ArrayList<GridMenuItem> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.layoutResourceId = resource;
        this.mGridData = objects;
    }

    public void setGridData(ArrayList<GridMenuItem> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.grid_menu_item_name);
            holder.imageView = (ImageView) convertView.findViewById(R.id.grid_menu_item_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GridMenuItem item = mGridData.get(position);
        holder.textView.setText(item.getTitle());
//        Glide.with(mContext).load(item.getImage()).into(holder.imageView);



        // 希望item大概50dp，垂直或者列间隔都为10，item又为正方形
//        ViewUtils.setGridViewItemWith(convertView, parent, 80, 10, 10, true);

        return convertView;
    }

    private class ViewHolder {
        TextView textView;
        ImageView imageView;
    }
}

